﻿using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace DeepMixWorkflow
{
    public class StreamFile
    {
        private readonly string _fileName;

        public StreamFile(string fileName)
        {
            _fileName = fileName;
        }

        public string FileName
        {
            get { return _fileName; }
        }

        public void DoWrite(Action<Stream> writeAction)
        {
            IsolatedStorageHelper.Do<object>(file =>
                {
                    using (
                        IsolatedStorageFileStream stream = file.OpenFile(_fileName, FileMode.Create, FileAccess.Write))
                    {
                        writeAction(stream);
                    }
                    return null;
                });
        }

        public void DoRead(Action<Stream> readAction)
        {
            IsolatedStorageHelper.Do<object>(file =>
                {
                    using (IsolatedStorageFileStream stream = file.OpenFile(_fileName, FileMode.Open))
                    {
                        readAction(stream);
                    }
                    return null;
                });
        }
    }
}